<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Solicitacoes extends CI_Controller {
	
	function __construct() {
		parent::__construct();
		
	}
	public function index(){
		$session_data = $this->session->userdata('grambel');
		$data["session_data"] = $session_data;

		$this->load->view('header');
		$this->load->view('view_ramal_sugestao');
	}
	public function salvarSolicitacao(){	
	
		if($this->input->post()){
		
			$soli = new Solicitacao();

			$soli->nomesolicitante = strtoupper($this->input->post("solicitante"));
			$soli->ramal = $this->input->post("ramal");
			$soli->email = strtoupper($this->input->post("email"));
			$soli->justificativa = strtoupper($this->input->post("justificativa"));
			
			if($soli->save()){
				
				$acao = $soli->check_last_query(false,true);
				$log = new Log();
				$log->ip =  $this->session->userdata('ip_address');
				$log->usuario = @$session_data["nome"];
				$log->acao = $acao;
				$log->save();
				
				echo 'SOLICITACAO REGISTRADA';
			}else{
				echo $ramal->error->string;
			}

		
		}else{
			echo 'ERRO - EMPTY';
		
		
		}
	
	}
	public function info($id = null){
		$session_data = $this->session->userdata('grambel');
		
		if($this->input->post()){
			$soli = new Solicitacao();
			$soli->where("id", $this->input->post("idsoli"))->get();
			if($soli->exists()){
				$this->input->post("idsoli");
				$soli->solucao = strtoupper($this->input->post("solucao"));
				$soli->status = "0";
				if($soli->save()){
				
					$acao = $soli->check_last_query(false,true);
					$log = new Log();
					$log->ip = $this->session->userdata('ip_address');
					$log->usuario = $session_data["nome"];
					$log->acao = $acao;
					$log->save();
					
					echo 'SOLICITAÇÃO FINALIZADA';
				}else{
					echo $soli->error->string;
				}
			}else{
			
				echo "SOLICITAÇÃO NÃO EXISTE";
			}

		}else{
			$soli = new Solicitacao();
			$soli->where("id", $id)->get();
			if($soli->exists()){
				$data["soli"] = $soli;
				$this->load->view('header');
				$this->load->view('view_solicitacao_info', $data);
			}
		}
	}
	public function solicitaAlteracao(){
		$data["titulopagina"] = 'Solicitar Alteração de Ramal';
		$this->load->view('header');
		$this->load->view('view_ramal_sugestao', $data);
	
	}
	public function sugestao(){

		$data["titulopagina"] = 'Sugestões';
		$this->load->view('header');
		$this->load->view('view_ramal_sugestao', $data);
	
	}
	public function atender($id){
		//******************************************************
		//   PERMISSAO 
		//******************************************************
		$session_data = $this->session->userdata('grambel');
		temPermissao(array('1','2'),$session_data);
		if(!empty($id)){
			$soli = new Solicitacao();
			$soli->where("id", $id)->get();
			if($soli->exists()){
				$soli->usuario_id = $session_data["usuario_id"];
				$soli->save();
				$this->load->view('header');
				redirect("solicitacoes/consultaSolicitacoes");
			}
		}
	
	}
	public function minhassolicitacoes(){
		//******************************************************
		//   PERMISSAO 
		//******************************************************
		$session_data = $this->session->userdata('grambel');
		temPermissao(array('1','2'),$session_data);
		
		$soli = new Solicitacao();
		//$soli->where("status","1")->where("usuario_id",$session_data["usuario_id"])->get();
		$soli->query("SELECT solicitacoes.id, 
		date_format(solicitacoes.dt_cadastro,'%d/%m/%Y %T') as dt_cadastro, 
		solicitacoes.nomesolicitante, 
		solicitacoes.ramal,
		solicitacoes.email, 
		solicitacoes.justificativa
		FROM `solicitacoes` WHERE `solicitacoes`.`status` =  '1' AND `solicitacoes`.`usuario_id` = '".$session_data["usuario_id"]."'");
		$data["solicitacao"] = $soli;
		
		$this->load->view('header');
		$this->load->view('view_solicitacao_meus', $data);
	}
	 public function consultahistoricosolicitacoes(){
		//******************************************************
		//   PERMISSAO 
		//******************************************************
		$session_data = $this->session->userdata('grambel');
		temPermissao(array('1','2'),$session_data);

		$this->load->view('header');
		$this->load->view('view_solicitacao_historico');
	}
	public function historicoJson(){
		//******************************************************
		//   PERMISSAO 
		//******************************************************
		$session_data = $this->session->userdata('grambel');
		temPermissao(array('1','2'),$session_data);
		
		$soli = new Solicitacao();

		echo $soli->historicosolicitacoes();
		
	}
	public function consultasolicitacoes(){
		//******************************************************
		//   PERMISSAO 
		//******************************************************
		$session_data = $this->session->userdata('grambel');
		temPermissao(array('1','2'),$session_data);
		
		$soli = new Solicitacao();
		//$soli->where("status","1")->where("usuario_id","IS NULL")->get();
		$soli->query("SELECT solicitacoes.id, 
		date_format(solicitacoes.dt_cadastro,'%d/%m/%Y %T') as dt_cadastro, 
		solicitacoes.nomesolicitante, 
		solicitacoes.ramal,
		solicitacoes.email, 
		solicitacoes.justificativa
		FROM `solicitacoes` WHERE `solicitacoes`.`status` =  '1' AND `solicitacoes`.`usuario_id` IS NULL");
		//echo $soli->check_last_query();
		$data["solicitacao"] = $soli;
		
		$this->load->view('header');
		$this->load->view('view_solicitacao_consulta', $data);
	}
	public function relatorioatendimento(){
		//******************************************************
		//   PERMISSAO 
		//******************************************************
		$session_data = $this->session->userdata('grambel');
		temPermissao(array('1','2'),$session_data);
		
		if($this->input->post()){
		
			//------------------------------------------------------------------------------------------------------------------------
		
			$pdf = new Pdf(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
			gerarRelatorio($pdf);
			$pdf->AddPage('L', 'A4');
			
			// TITULO DA PAGINA
			$pdf->Write(0, 'CONTROLE DE TELEFONEMAS', '', 0, 'C', true, 0, false, false, 0);
			$pdf->Ln();
			// TITULO DA PAGINA
			$pdf->Write(0, 'Periodo: '.$this->input->post('inicio').' a '.$this->input->post('fim'), '', 0, 'C', true, 0, false, false, 0);
			$pdf->Ln();
			
			$ate = new Atendimento();
			$ate = $ate->relatorioAtendimento($this->input->post('tipoligacao'),brToUsa($this->input->post('inicio')),brToUsa($this->input->post('fim')));
			

			//*************************************************************************************
			// CABEÇALHO
			//*************************************************************************************
			$header = array('Data','Ramal','Solicitante','Telefone','Solicitado','Assunto','DDD','Responsavel');
			// Colors, line width and bold font
			$pdf->SetFillColor(208, 208, 208);
			$pdf->SetTextColor(0);
			$pdf->SetDrawColor(0, 0, 0);
			$pdf->SetLineWidth(0.3);
			$pdf->SetFont('times', 'B', 7);
			
			// Header
			$w = array(23, 20, 40,20, 40, 50,20, 50);
			$num_headers = count($header);
			for($i = 0; $i < $num_headers; ++$i) {
				$pdf->Cell($w[$i], 7, $header[$i], 1, 0, 'C', 1);
			}
			$pdf->Ln();
			//*************************************************************************************
			// DADOS
			//*************************************************************************************
			// Color and font restoration
			$pdf->SetFillColor(224, 235, 255);
			$pdf->SetTextColor(0);
			$pdf->SetFont('times', '', 7);
			// Data
			$fill = 0;

			foreach($ate as $r){	
			
				$pdf->Cell($w[0], 6,$r->dt_cadastro, 'LR', 0, 'L', $fill);
				$pdf->Cell($w[1], 6,$r->ramal, 'LR', 0, 'L', $fill);
				$pdf->Cell($w[2], 6,$r->solicitante, 'LR', 0, 'L', $fill);
				$pdf->Cell($w[3], 6,$r->telefone, 'LR', 0, 'L', $fill);
				$pdf->Cell($w[4], 6,$r->solicitado, 'LR', 0, 'L', $fill);
				$pdf->Cell($w[5], 6,$r->assunto, 'LR', 0, 'L', $fill);
				$pdf->Cell($w[6], 6,$r->tipoligacao, 'LR', 0, 'L', $fill);
				$pdf->Cell($w[7], 6,$r->nome_completo, 'LR', 0, 'L', $fill);
				$pdf->Ln();
				$fill=!$fill;
				
			}
			// FINALIZA TABELA
				$pdf->Cell(array_sum($w), 0, '', 'T');
			
				
		
		/*
		$html = "";
		$pdf->writeHTML($html, true, false, true, false, '');
		*/
		
		$pdf->Output('Relatorio_Devolucao.pdf', 'I');
			
			
		}else{
				
			
			/*$secretaria = new Secretaria();
			$data["secretaria"] = $secretaria->consultaSecretaria();*/
			
			$this->load->view('header');
			$this->load->view('view_atendimento_relatorio');
		}
	}
	
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */